package com.infore.eureka;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Discription TODO
 * @Autor lijianhua
 * @Date 2020/2/19 11:26
 */
@EnableEurekaServer
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class EurekaApplication extends SpringBootServletInitializer implements CommandLineRunner {
    protected static Log log = LogFactory.getLog(EurekaApplication.class);

    /**
     * 如果要使用tomcat来加载jsp的话就必须继承SpringBootServletInitializer类并且重写其中configure方法
     * @param application
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(EurekaApplication.class);
    }

    public static void main(String[] args)  throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(EurekaApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path")==null? "":env.getProperty("server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n" +
                "Application service is running! Access URLs:\n" +
                "eureka地址: \thttp://" + ip + ":" + port + path  +"\n"+
                "----------------------------------------------------------");
    }

    /**
     * springboot运行后此方法首先被调用<br/>
     * 实现CommandLineRunner抽象类中的run方法
     */
    @Override
    public void run(String... args) throws Exception {
        System.out.println("-----------------------springboot启动完成！" + Thread.currentThread().getName());
    }
}
